package com.bifang.common.util;

import com.bifang.common.vo.SessionInfo;
import java.util.HashSet;
import java.util.Set;

/**
 * session 工具
 *
 * @author chenzh
 * @date 2018-08-14
 */
public class SessionUtil {
    private static final ThreadLocal<SessionInfo> loginInfoThreadLocal =
            new ThreadLocal<SessionInfo>() {
                @Override
                protected SessionInfo initialValue() {
                    return null;
                }
            };

    public SessionUtil() {}

    public static void setSessionInfo(SessionInfo sessionInfo) {
        loginInfoThreadLocal.set(sessionInfo);
    }

    public static SessionInfo getSessionInfo() {
        return (SessionInfo) loginInfoThreadLocal.get();
    }

    public static void removeSesionInfo() {
        loginInfoThreadLocal.remove();
    }

    public static String getUserId() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getUserId();
    }

    public static String getUserName() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getUserName();
    }

    public static String getFullName() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getFullName();
    }

    public static String getOrgId() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getOrgId();
    }

    public static String getUserType() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getUserType();
    }

    public static Set<String> getSystemPrivileges() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? new HashSet<>() : sessionInfo.getSystemPrivileges();
    }

    public static Set<String> getRoles() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? new HashSet<>() : sessionInfo.getRoles();
    }

    public static String getLoginType() {
        SessionInfo sessionInfo = getSessionInfo();
        return sessionInfo == null ? "" : sessionInfo.getLoginType();
    }
}
